package gspushmq.v1.thread;

import java.io.FileInputStream;
import java.util.Properties;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import gspushmq.model.GsConfig;


public class GGThreadPoolBuilder {
	public static void start() {
		
		
		try {
			
			FileInputStream in = new FileInputStream("config/config.properties");
			
			Properties p = new Properties();
			p.load(in);
			
			Integer core_size = Integer.valueOf(p.getProperty("ggthreadpool.coresize")); 
			Integer max_size =  Integer.valueOf(p.getProperty("ggthreadpool.maxsize"));
			Integer queue_size = Integer.valueOf(p.getProperty("ggthreadpool.queuesize"));
			ThreadPoolExecutor threadPool = 
					new ThreadPoolExecutor(
						//核心线程池大小为Ncpu
						core_size,
						//最大线程池大小为2*Ncpu
						max_size, 
					    60, 
					    TimeUnit.SECONDS,
					    new PriorityBlockingQueue<Runnable>(queue_size));
			GGThreadPoolExecutor.pools.init(threadPool);
			
			//注入配置
			loadAppKey(p);
			
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}  
	}
	
	/**
	 * 配置信息
	 * @param p
	 * @author yutao
	 * @date 2018年11月1日上午10:30:37
	 */
	private static void loadAppKey(Properties p) {
		String domain = p.getProperty("api.domain");
		String appKey = p.getProperty("api.appKey");
		String appSecret = p.getProperty("api.appSecret");
		
		GsConfig.config.setDomain(domain);
		GsConfig.config.setAppKey(appKey);
		GsConfig.config.setAppSecret(appSecret);
	}
	
	
	public static void destory() {
		GGThreadPoolExecutor.pools.destory();
	}

}
